package com.uzai.console.web.log;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.dto.log.userloginlog.ForcedLogoutDto;
import com.uzai.console.dto.log.userloginlog.UserLoginLogCleanDto;
import com.uzai.console.dto.log.userloginlog.UserLoginLogQuery;
import com.uzai.console.dto.sys.datahandle.WithdrawRecordCleanDto;
import com.uzai.console.dto.uzaichat.kefuuser.KefuUserQuery;
import com.uzai.console.entity.KefuUser;
import com.uzai.console.entity.Merchant;
import com.uzai.console.entity.es.EsUserLoginLog;
import com.uzai.console.mapper.KefuUserMapper;
import com.uzai.console.mapper.MerchantMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.log.UserLoginLogService;
import com.uzai.console.service.merchant.LoginService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 登录日志
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "登录日志",tags = "查看日志|登录日志")
@ApiSort(26)
@RestController
public class UserLoginLogController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(UserLoginLogController.class);

    @Autowired
    private UserLoginLogService userLoginLogService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private KefuUserMapper kefuUserMapper;
    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "查询账号登录日志列表", notes = "查询账号登录日志列表", response = EsUserLoginLog.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/userLoginLog/findUserLoginLogList")
    public Object findUserLoginLogList(@RequestBody UserLoginLogQuery userLoginLogQuery){
        return userLoginLogService.findUserLoginLogList(userLoginLogQuery);
    }


    @ApiOperation(value = "查询账号列表", notes = "查询账号列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/userLoginLog/findAccountList")
    public Object findAccountList(){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        //先封装主账号
        Merchant merchant = merchantMapper.selectById(getLoginInfo().getMerId());
        SelectKeyVo selectKeyVo = new SelectKeyVo();
        selectKeyVo.setKey(merchant.getMobile());
        selectKeyVo.setValue(merchant.getName());
        selectKeyVoList.add(selectKeyVo);

        KefuUserQuery kefuUserQuery = new KefuUserQuery();
        kefuUserQuery.setMerId(getLoginInfo().getMerId());
        List<KefuUser> userList = kefuUserMapper.findByList(kefuUserQuery);
        if(userList != null && userList.size() > 0){
            for(KefuUser kefuUser : userList){
                SelectKeyVo selectKeyVo_kefu = new SelectKeyVo();
                selectKeyVo_kefu.setKey(kefuUser.getUser());
                selectKeyVo_kefu.setValue(kefuUser.getName());
                selectKeyVoList.add(selectKeyVo_kefu);
            }
        }

        return selectKeyVoList;
    }

    @ApiOperation(value = "强制登录账户下线", notes = "强制登录账户下线", response = Boolean.class)
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/userLoginLog/forcedLogout")
    public Object forcedLogout(@RequestBody ForcedLogoutDto forcedLogoutDto) {

        List<String> loginTokenList = forcedLogoutDto.getLoginTokenList();

        if(loginTokenList == null || loginTokenList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入需要强制下线的token");
        }

        for (String loginToken : loginTokenList) {
            try {
                //清空redis中的登录信息
                loginService.logout(loginToken);
            }catch (Exception e){

            }
        }
        return true;
    }

    @ApiOperation(value = "清空登录日志", notes = "清空登录日志", response = Boolean.class)
    @PostMapping("/userLoginLog/clean")
    public Object userLoginLogClean(@RequestBody UserLoginLogCleanDto userLoginLogCleanDto) {

        if (Objects.isNull(userLoginLogCleanDto) || StringUtils.isAnyBlank(userLoginLogCleanDto.getVerifyCode(), userLoginLogCleanDto.getMobile())) {
            throw new BusinessException("清空登录日志请求参数错误");
        }

        boolean result = userLoginLogService.userLoginLogClean(userLoginLogCleanDto);
        logger.info("--userLoginLogClean result is: {}", result);
        return result;
    }

}
